﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
namespace Nirvana
{
    [CustomEditor(typeof(I18NChineseToPOT))]
    public class I18NChineseToPOTEditor : Editor
    {
        private SerializedProperty language;
        private SerializedProperty poOutPath;


        private SerializedProperty scriptPath;
        private SerializedProperty scriptMatchRegex;
        private SerializedProperty ingoreFolderAndFile, ingoreLogAndOther;

        private SerializedProperty uiPrefabPath;
        private SerializedProperty uiMatchRegex;
        private SerializedProperty ignoreUIFolder;

        private SerializedProperty xmlPath;
        private SerializedProperty xmlRegex, isOpenLog;
        private SerializedProperty ingoreXmlFile;

        private SerializedProperty BugsAddress;
        private SerializedProperty CopyrightHolder;
        private SerializedProperty CopyrightYear;
        private SerializedProperty PackageName;
        private SerializedProperty PackageVersion;

        private static bool isHold1, isHold2, isHold3, isHold4;
        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;

            this.language = serializedObject.FindProperty("language");

            this.scriptPath = serializedObject.FindProperty("scriptPath");
            this.scriptMatchRegex = serializedObject.FindProperty("scriptMatchRegex");
            this.ingoreFolderAndFile = serializedObject.FindProperty("ingoreFolderAndFile");
            this.ingoreLogAndOther = serializedObject.FindProperty("ingoreLogAndOther");


            this.uiPrefabPath = serializedObject.FindProperty("uiPrefabPath");
            this.uiMatchRegex = serializedObject.FindProperty("uiMatchRegex");
            this.ignoreUIFolder = serializedObject.FindProperty("ignoreUIFolder");

            this.xmlPath = serializedObject.FindProperty("xmlPath");
            this.xmlRegex = serializedObject.FindProperty("xmlRegex");

            this.ingoreXmlFile = serializedObject.FindProperty("ingoreXmlFile");
            this.isOpenLog = serializedObject.FindProperty("isOpenLog");


            this.poOutPath = serializedObject.FindProperty("poOutPath");
            this.BugsAddress = serializedObject.FindProperty("BugsAddress");
            this.CopyrightHolder = serializedObject.FindProperty("CopyrightHolder");
            this.CopyrightYear = serializedObject.FindProperty("CopyrightYear");
            this.PackageName = serializedObject.FindProperty("PackageName");
            this.PackageVersion = serializedObject.FindProperty("PackageVersion");

        }



        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            //GUI.color = Color.cyan;
            EditorGUILayout.HelpBox("\n中文翻译提取工具，用于提取Lua代码，Prefab，XML配置表中的中文。\n\n" +
                "POT文件是种语言包文件,其中包含原始语⾔和被翻译的⽬标语⾔。\n\n" +
                "一般使用Poedit编辑器打开来快速翻译，输出压缩后的二进制.MO文件\n", MessageType.Info);

            EditorGUILayout.Space();

            GUILayout.Label("POT文件信息头", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.BugsAddress, new GUIContent("Bug提交Email"));
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.CopyrightHolder, new GUIContent("作者"));
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.CopyrightYear, new GUIContent("创建时间"));
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.PackageName, new GUIContent("项目名"));
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.PackageVersion, new GUIContent("项目版本号"));
            EditorGUILayout.Space();
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.language);
            EditorGUILayout.PropertyField(this.poOutPath);
            EditorGUILayout.Space();

            DrawScriptUI();
            EditorGUILayout.Space();
            EditorGUILayout.Space();

            DrawUIPrefabUI();
            EditorGUILayout.Space();
            EditorGUILayout.Space();

            DrawXmlUI();
            EditorGUILayout.Space();
            EditorGUILayout.Space();



            serializedObject.ApplyModifiedProperties();
        }


        private void DrawScriptUI()
        {
            isHold1 = EditorGUILayout.Foldout(isHold1, "Script To Chinese");
            if (isHold1)
            {
                isHold2 = false;
                isHold3 = false;
                isHold4 = false;
                EditorGUILayout.PropertyField(this.scriptPath);
                EditorGUILayout.PropertyField(this.scriptMatchRegex, new GUIContent("Match Regex"));
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(this.ingoreFolderAndFile);
                EditorGUILayout.PropertyField(this.ingoreLogAndOther);
                EditorGUILayout.Space();

                if (GUILayoutEx.ActionButton("输出脚本中文"))
                {

                    I18NChineseToPOT rule = this.target as I18NChineseToPOT;
                    rule.ScriptToChinese();
                }

                if (GUILayoutEx.ActionButton("打印Mo文件"))
                {

                    I18NChineseToPOT rule = this.target as I18NChineseToPOT;
                    rule.Load();
                }
            }
        }

        private void DrawUIPrefabUI()
        {
            isHold2 = EditorGUILayout.Foldout(isHold2, "UI Prefab To Chinese");
            if (isHold2)
            {
                isHold1 = false;
                isHold3 = false;
                isHold4 = false;
                EditorGUILayout.PropertyField(this.uiPrefabPath);
                EditorGUILayout.PropertyField(this.uiMatchRegex, new GUIContent("Match Regex"));
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(this.ignoreUIFolder);
                EditorGUILayout.Space();

                if (GUILayoutEx.ActionButton("输出UI Prefab中文"))
                {

                    I18NChineseToPOT rule = this.target as I18NChineseToPOT;
                    rule.UIPrefabToChinese();
                }
            }
        }

        private void DrawXmlUI()
        {
            isHold3 = EditorGUILayout.Foldout(isHold3, "Xml To Chinese");
            if (isHold3)
            {
                isHold2 = false;
                isHold1 = false;
                isHold4 = false;
                EditorGUILayout.PropertyField(this.xmlPath);
                EditorGUILayout.PropertyField(this.xmlRegex, new GUIContent("Xml Match Regex"));
                EditorGUILayout.PropertyField(this.isOpenLog);
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(this.ingoreXmlFile);
                EditorGUILayout.Space();

                if (GUILayoutEx.ActionButton("输出Xml表中文"))
                {

                    I18NChineseToPOT rule = this.target as I18NChineseToPOT;
                    rule.XmlToChinese();
                }
            }
        }



    }
}

